204. 计数质数
为保证权益,题目请参考 204. 计数质数(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int countPrimes(int n) {
vector<bool> dp(n, true);
int ans = 0;
for (int i = 2; i < n; i++) {
if(dp[i]){
ans ++;
for(int j = 2*i;j<n;j+=i){
dp[j] = false;
}
}
}
return ans;
}
};
int main() {
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25